#include "guiterm.hpp"
#ifndef FILEVIEW_HPP
#define FILEVIEW_HPP
#include <QTreeWidget>
#include <QListWidget>
#include "LsCommand.hpp"
#include "Path.hpp"

class LsListReceiver;
class FileListWidget;

struct FileEntry
{
	vector<string> fields;
};

class FileView
	:public JobViewSegment
{
	Q_OBJECT
public:
	FileView(RemoteJob *job);
	virtual ~FileView();
	void beginRefresh(ServerConnection *host, const RPCParams *params);
	
protected slots:
	void openSettingsMenu();
	void doubleClicked(string fileName, string fileType);
	void pressed(string fileName, string fileType);
	void selectionChanged(const vector<string> &selectedFiles);
	
	void setDetailed(bool detailed);
	void setShowHidden(bool showHidden);
	
	LsCommand getCommand() const;
	void setCommand(LsCommand command);
	
protected:
	void initListWidget(const RPCParams *params);
	void requestRefresh();
	string expandFileName(string fileName);
	
	LsCommand command;
	Path path;
	
	QVBoxLayout *layout;
	QHBoxLayout *headerLayout;
	QLabel *commandLabel;
	LinkWidget *settingsLink;
	
	LsListReceiver *listReceiver;
	FileListWidget *listWidget;
};

class LsListReceiver
	:public QObject, public StreamReceiver
{
	Q_OBJECT
public:
	LsListReceiver(RemoteHost *source, const RPCParams *params);
	void recv(int len, const char *data);
	void onClose();
signals:
	void filesAdded(const vector<FileEntry> &files);
	void listFinished();
private:
	void parseLine(const char *data, int len, vector<FileEntry> &files);
	StreamBuffer buf;
};

class FileListWidget
	:public QWidget
{
	Q_OBJECT
public:
	FileListWidget(const RPCParams *params, QWidget *parent=0);
	
signals:
	void doubleClicked(string fileName, string fileType);
	void pressed(string fileName, string fileType);
	void selectionChanged(const vector<string> &selectedFiles);
	
public slots:
	int getFieldIndex(string field);
	string getFieldName(int index);
	string getFileName(const FileEntry *file);
	string getFileType(const FileEntry *file);
	virtual void filesAdded(const vector<FileEntry> &files)=0;
	
protected:
	vector<string> fields;
	map<string,string> fileTypes;
};

class DetailedFileView
	:public FileListWidget
{
	Q_OBJECT
public:
	DetailedFileView(const RPCParams *params, int width, QWidget *parent=0);
	QSize sizeHint() const;
	
public slots:
	void itemDoubleClicked(QTreeWidgetItem *item, int column);
	void itemPressed(QTreeWidgetItem *item, int column);
	void itemSelectionChanged();
	void filesAdded(const vector<FileEntry> &files);
	
private:
	QTreeWidget *tree;
	vector<int> columnFields;
};

class ColumnLayout
	:public QVBoxLayout
{
	Q_OBJECT
public:
	ColumnLayout(QListWidget *list, int numFiles, string colSpec);
	bool hasHeightForWidth() const;
	int heightForWidth(int w) const;
	
private:
	vector<int> colWidths;
	int numFiles;
	QListWidget *list;
};

class ColumnFileView
	:public FileListWidget
{
	Q_OBJECT
public:
	ColumnFileView(const RPCParams *params, int width, QWidget *parent=0);
	
public slots:
	void itemDoubleClicked(QListWidgetItem *item);
	void itemPressed(QListWidgetItem *item);
	void itemSelectionChanged();
	void filesAdded(const vector<FileEntry> &files);
	
private:
	QListWidget *list;
};


#endif
